<template>
	<view class="content">
		<view class="form-block" style="border-radius:30rpx 30rpx 0 0 ;">
			<view class="form-title">编辑工作经历</view>
			<view class="form-item">
				<view class="form-label">公司名称</view>
				<view class="form-content">
					<input v-model="form.companyName" maxlength="30" type="text" placeholder-class="inputPlaceholder"
						placeholder="请填写公司名称" class="form-ctrl" />
					<view class="iconfont icon-gengduo1 arrow"></view>
				</view>
			</view>
			<!-- <view class="form-item">
				<view class="form-label">屏蔽该公司</view>
				<view class="form-content">
					对该公司屏蔽我的简历<switch :checked="form.shield" style="scale: 0.7;" class="form-ctrl" @change="shieldChange" />
				</view>
			</view> -->
			<view class="form-item">
				<view class="form-label">所属行业</view>
				<view class="form-content">
					<picker class="form-ctrl" :range="industry" range-key="data" :value="industryIndex"
						@change="bindIndustry">{{industry[industryIndex]['data']}}</picker>
					<view class="iconfont icon-gengduo1 arrow"></view>
				</view>
			</view>
			<view class="form-item">
				<view class="form-label">任职时间</view>
				<view class="form-content">
					<picker mode="date" class="form-ctrl" fields="day" :end="end" :value="form.workTimeStart"
						@change="bindStart">
						<view class="uni-input">{{form.workTimeStart}}</view>
					</picker>
					<picker mode="date" class="form-ctrl" fields="day" :start="form.workTimeStart" :end="end"
						:value="form.workTimeEnd" @change="bindEnd">
						<view class="uni-input">{{form.workTimeEnd}}</view>
					</picker>
					<view class="iconfont icon-gengduo1 arrow"></view>
				</view>
			</view>
			<view class="form-item">
				<view class="form-label">职位名称</view>
				<view class="form-content">
					<input v-model="form.postName" maxlength="30" type="text" placeholder-class="inputPlaceholder"
						placeholder="请填写职位名称" class="form-ctrl" />
					<view class="iconfont icon-gengduo1 arrow"></view>
				</view>
			</view>
			<view class="form-item">
				<view class="form-label">职位类别</view>
				<view class="form-content">
					<input v-model="form.postTypeText" maxlength="30" type="text" placeholder-class="inputPlaceholder"
						disabled="true" @click="chooseJobCategory" placeholder="请选择职位类别" class="form-ctrl" />
					<view class="iconfont icon-gengduo1 arrow"></view>
				</view>
			</view>
			<view class="form-item">
				<view class="form-label">职责业绩</view>
				<view class="form-content">
					<textarea :auto-height="true" style="line-height: normal;min-height: 300rpx;padding: 30rpx 0;"
						v-model="form.duty" maxlength="500" type="text" placeholder-class="inputPlaceholder"
						placeholder="请填写职责业绩" class="form-ctrl"></textarea>
				</view>
			</view>
			<view class="form-item">
				<view class="form-label">工作内容</view>
				<view class="form-content">
					<textarea :auto-height="true" style="line-height: normal;min-height: 300rpx;padding: 30rpx 0;"
						v-model="form.workExplain" maxlength="500" type="text" placeholder-class="inputPlaceholder"
						placeholder="请填写工作内容" class="form-ctrl"></textarea>
				</view>
			</view>
			<view class="btn" @click="handleSave">保存</view>
		</view>
	</view>
</template>

<script>
	import {
		validate
	} from '@/util/validate.js';
	import {
		getIndustry as getIndustryApi,
		getJobCategory as getJobCategoryApi
	} from '@/api/config.js'
	import {
		submitWorkExp,
		getWorkExp
	} from '@/api/resume.js'
	import {
		getCurrentTime,
		getPickerIndex
	} from '@/util/util';
	export default {
		data() {
			return {
				form: {
					companyName: '',
					shield: false,
					postName: '',
					workTimeStart: getCurrentTime('d'),
					workTimeEnd: getCurrentTime('d'),
					industry: null,
					postType: null,
					id: 0,
					duty: '',
					workExplain: '',
					postTypeText: '',
				},
				rules: {
					companyName: {
						name: '公司名称',
						required: true
					},
					industry: {
						name: '所属行业',
						required: true
					},
					workTimeStart: {
						name: '任职时间',
						required: true
					},
					workTimeEnd: {
						name: '任职时间',
						required: true
					},
					postName: {
						name: '职位名称',
						required: true
					},
					postType: {
						name: '职位类别',
						required: true
					},
					duty: {
						name: '职责业绩',
						required: true
					},
					workExplain: {
						name: '工作内容',
						required: true
					}
				},
				end: getCurrentTime('d'),
				industry: [],
				industryIndex: 0,
			}
		},
		methods: {
			bindStart(e) {
				this.form.workTimeStart = e.detail.value
			},
			bindEnd(e) {
				this.form.workTimeEnd = e.detail.value
			},
			shieldChange(e) {
				this.form.shield = e.detail.value
			},
			chooseJobCategory() {
				uni.navigateTo({
					url: '/page_other/job_category/index?max=1'
				})
			},
			bindIndustry(e) {
				this.industryIndex = e.detail.value
			},
			handleSave() {
				let form = JSON.parse(JSON.stringify(this.form));
				form.shield = form.shield ? 1 : 2;
				form.industry = this.industry[this.industryIndex].id
				if (validate(form, this.rules)) {
					uni.showLoading({
						title: '提交中',
						mask: true
					})
					submitWorkExp(form).then(() => {
						uni.$emit('refresh', 2)
						this.showToast('保存成功', () => {
							uni.navigateBack()
						})
					})
				}
			},
			getDetail(id) {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				getWorkExp({
					id
				}).then((res) => {
					res.shield = res.shield === 1;
					this.industryIndex = getPickerIndex(res.industry, this.industry)
					this.form = res
				})
			},
			getIndustry(id) {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				getIndustryApi({}).then((res) => {
					this.industry = res
					if (id) {
						this.getDetail(id)
					}
				})
			}
		},
		onLoad(e) {
			this.getIndustry(e.id)
			uni.$on('getJobCategory', (e) => {
				this.form.postType = e[0].id;
				this.form.postTypeText = e[0].jobTitle
			})
		}

	}
</script>

<style lang="scss">
	page {
		background: #fff;
	}

	.content {
		width: 100%;

		.btn {
			width: 350rpx;
			height: 80rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			background: #FF7626;
			color: #fff;
			font-size: 28rpx;
			border-radius: 40rpx;
			margin: 0 auto;
			margin-top: 30rpx;
		}

		.user-photo-block {
			width: 100%;
			height: 450rpx;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;

			.user-photo {
				width: 180rpx;
				height: 180rpx;
				border-radius: 50%;
			}
		}

		.form-block {
			width: 100%;
			box-shadow: 0 0 10px 0 #efefef;
			background: #fff;
			padding: 30rpx;

			.form-title {
				font-size: 36rpx;
				font-weight: bold;
			}

			.sub-title {
				font-size: 26rpx;
				color: #999;
			}

			.form-item {
				display: flex;
				flex-direction: column;
				border-bottom: 1px solid #efefef;
				padding-top: 30rpx;

				.form-label {
					font-size: 28rpx;
					color: #999;
				}

				.form-content {
					display: flex;
					flex-direction: row;
					align-items: center;

					.form-ctrl {
						height: 100rpx;
						line-height: 100rpx;
						flex-grow: 1;
					}

					.arrow {
						flex-grow: 0;
						color: #999;
						font-size: 24rpx;
					}
				}
			}
		}
	}
</style>